package business;

import org.apache.log4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// represent a worker of the bank
// has a queue to work with user request
public class BankCashier implements Cashier {
    // id of the worker
    private final int _id;
    // bank, where cashier is working;
    private final Bank _bank;
    // cashier full name
    private final ExecutorService _cashierExecutorService;
    // logger
    private static final Logger LOGGER = Logger.getLogger(BankCashier.class);

    // get an id of the cashier
    public int getId() {
        return _id;
    }

    // get bank of the cashier
    public Bank getBank() {
        return _bank;
    }

    // full constructor
    public BankCashier(int id, Bank bank, ExecutorService cashierExecutorService) {
        if (bank == null) {
            throw new NullPointerException();
        }
        _bank = bank;

        if (cashierExecutorService == null) {
            throw new NullPointerException();
        }
        _cashierExecutorService = cashierExecutorService;

        _id = id;
    }

    // constructor with single thread executor
    public BankCashier(int id, Bank bank) {
        this(id, bank, Executors.newSingleThreadExecutor());
    }

    // enqueue operation
    public void enqueueOperation(BankOperation operation) {
        if (operation == null) {
            throw new NullPointerException();
        }
        _cashierExecutorService.execute(operation);
    }

    // stop enqueueing operations
    public void stopWorkingDay() {
        _cashierExecutorService.shutdown();
    }
}
